package com.keehin.migrate;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.sql.SQLException;

import com.keehin.Database;

public class ImportCSV2 {

	protected static Database destDB;

	public static void main(String[] args) throws Exception {
		try {
			destDB = new Database(new com.mysql.jdbc.Driver(), "jdbc:mysql://"
					+ args[0] + "?characterEncoding=tis620", args[1], args[2]);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (destDB != null)
			System.out.println(destDB + "OK");

		File inFile = new File(args[3]);
		FileInputStream in = new FileInputStream(inFile);
		InputStreamReader x = new InputStreamReader(in,
				Charset.forName("UTF-8"));
		BufferedReader br = new BufferedReader(x);
		String line = br.readLine();
		int i = 1;
		String sql = "insert into " + args[4] + " values(";
		while (line != null) {
			destDB.executeUpdate(sql + line + ")");
			printSymbol(i++);
			line = br.readLine();
		}
		br.close();
		x.close();
		in.close();
		destDB.getConnection().close();
	}

	public static void printSymbol(int i) {
		if (i % 500 == 0) {
			System.out.print("#");
			if (i % 100000 == 0)
				System.out.printf("%,10d\n", i);
			else if (i % 50000 == 0)
				System.out.println();
		}
	}
}
